package hn.cch.aspect;

import hn.cch.constant.Common;
import hn.cch.http.request.HttpReqMsg;
import hn.cch.util.JsonUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(5)
public class ParamAop {

    private Logger logger = LoggerFactory.getLogger(ParamAop.class);

//    ProceedingJoinPoint:Around
//    JoinPoint:Before|After
    @Before("execution(* hn.cch.http.api.*.api(..))")
    public void aop(JoinPoint joinPoint) {


        logger.info("ParamAop start");
        Object[] args = joinPoint.getArgs();
        HttpReqMsg httpReqMsg = null;
        byte[] bytes = null;
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof HttpReqMsg) {
                httpReqMsg = (HttpReqMsg) args[i];
            }
            if (args[i] instanceof byte[]) {
                bytes = (byte[]) args[i];
            }
        }
        if (bytes == null) {
            logger.error("ParamAop error");
        }
        if (httpReqMsg == null) {
            logger.error("ParamAop error");
        }
        // 参数转化
//        格式：path|type
        String param = httpReqMsg.getGet().getPath() + "|" + httpReqMsg.getGet().getType();
        Object object = JsonUtil.toObject(bytes, Common.params.get(param));

        /******************************************************/
//        Object result = null;
//        try {
//            result = joinPoint.proceed(new Object[]{httpReqMsg, bytes});
//        } catch (Throwable e) {
//            e.printStackTrace();
//            logger.error("ParamAop fail");
//        }
        /******************************************************/

        logger.error("ParamAop success");
    }
}
